//
//  HLSongViewModel.m
//  HLMusicBox
//
//  Created by hulinhu on 2018/1/2.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

#import "HLSongViewModel.h"

@implementation HLSongViewModel

-(RACCommand *)songListCommand {
    
    if (!_songListCommand) {
        _songListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *partOne = @"GET:/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.billboard.billList&format=json&";
                NSString *partTwo = [NSString stringWithFormat:@"type=%lu&offset=%lu&size=%d",[[input objectForKey:@"type"] integerValue],[[input objectForKey:@"offset"] integerValue],20];
                NSString *urlString = [partOne stringByAppendingString:partTwo];
                // 网络请求
                [HLNetWork requestNetWorkURL:urlString parmas:nil cacheTimeInSeconds:0 completion:^(id result) {
                    NSArray *array = [result objectForKey:@"song_list"];
                    [subscriber sendNext:array];
                    [subscriber sendCompleted];
                } failed:^(id result) {
                    NSLog(@"error -- %@",result);
                }];
                return nil;
            }];
        }];
    }
    return _songListCommand;
}

-(RACCommand *)songItemCommand {
    
    if (!_songItemCommand) {
        _songItemCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *songID) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *partOne = @"GET:/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.song.play&songid=";
                NSString *urlString = [partOne stringByAppendingString:songID?songID:@""];
                // 网络请求
                [HLNetWork requestNetWorkURL:urlString parmas:nil cacheTimeInSeconds:0 completion:^(id result) {
                    [subscriber sendNext:result];
                    [subscriber sendCompleted];
                } failed:^(id result) {
                    NSLog(@"error -- %@",result);
                }];
                return nil;
            }];
        }];
    }
    return _songItemCommand;
    
}

@end
